{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-20T21:39:51.448472Z",
     "end_time": "2023-04-20T21:39:51.686222Z"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 原理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 预测"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "逻辑回归不同于线性回归直接输出结果，它输出的是结果的数理逻辑值：$p = \\sigma(\\mathbf{w}\\mathbf{x} + b)$，$p$可以理解为对y=1概率的预测\n",
    "\n",
    "其中$\\sigma$是一个sigmoid函数，$\\sigma(z) = \\frac{1}{1 + e^{-z}}$，如下图："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": [],
    "ExecuteTime": {
     "start_time": "2023-04-20T21:39:51.687828Z",
     "end_time": "2023-04-20T21:39:51.804605Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 500x200 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAb8AAADcCAYAAADgBK8yAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA7i0lEQVR4nO3deVhUZfsH8O8szLBvIssgAgriLqaCmGkmCmalvS1qi0ippbYYlsWbStZbJ9zSzDfLVOy13MqlnxouJOaCqCiKigsKIsIAsi8yw8x5fn8QR0fWUWCAuT/XNRfMc+7zzD3PLPecXcQYYyCEEEKMiNjQCRBCCCEtjYofIYQQo0PFjxBCiNGh4kcIIcToUPEjhBBidKj4EUIIMTpU/AghhBgdKn6EEEKMDhU/QgghRoeKHyEtbMqUKfDw8DB0Gv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t9TUVJ3nlJ6ezoYNG8bs7e2ZXC5nXl5e7KOPPmqR4/wSEhKYv78/s7GxYaampqxHjx7sq6++0lmKfjBfxhi7e/cumzlzJrOzs2Pm5ubs+eef1yk+zWn9+vW1vkfuX0li6DFeuXIl69y5M5PJZMzPz4+dOHFCmDZ8+HAWEhKiE79161bWrVs3JpPJWK9evdiePXuaJa8H1TWO69evrzPf2bNnC8/NycmJPf300+zMmTMtki9jjE2YMIG5uLgwmUzGXF1d2YQJE1hKSkqd+TJmuPGttm/fPgaAXblypcY0Q49v9fflg7fqnHieZ/Pnz2dOTk5MLpezkSNH1nge7u7uLCIiQqetvs9AY9EljQghhBgdOs6PEEKI0aHiRwghxOhQ8SOEEGJ0qPgRQggxOlT8CCGEGB0qfoQQQowOFT9CCCFGh4ofIYQQo0PFjxBCiNGh4kcIIcToUPEjhBBidKj4EUIIMTr/D7bgG30/fajKAAAAAElFTkSuQmCC"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = np.arange(-10, 10, 0.1)\n",
    "y = 1 / (1 + np.e**(-x))\n",
    "plt.figure(figsize=(5,2)) #要放在plt.plot之前\n",
    "plt.plot(x, y)\n",
    "plt.axhline(y=0.5, color='grey', linestyle=':')\n",
    "plt.axhline(y=1, color='grey', linestyle=':') #y=0.5,1 分别画一条线\n",
    "plt.axvline(x=0, color='black', linestyle='-') #x=0 画一条线\n",
    "plt.title(\"sigmoid\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一旦逻辑回归模型估算出实例x属于正类的概率 $p=\\sigma(\\hat{z})$ ，就可以轻松预测出 $\\hat{y}$：\n",
    "$$\\hat{y} = \\begin{cases}\n",
    "0 & ,p < 0.5 \\\\\n",
    "1 & ,p \\geq 0.5\n",
    "\\end{cases}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 训练和成本函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "训练的目的就是设置参数向量θ,使模型对正类实例做出高概率估算(y=1),对负类实例做出低概率估算(y=0)。 下面公式所示为单个训练实例x的成本函数,正说明了这一点。\n",
    "$$J(\\theta) = \\begin{cases}\n",
    "-log(\\hat{p}) & ,y=1 \\\\\n",
    "-log(1-\\hat{p}) & ,y=0\n",
    "\\end{cases}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-20T21:36:03.026056Z",
     "end_time": "2023-04-20T21:36:03.103803Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 400x200 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAVwAAADFCAYAAAAYCEoTAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAsuUlEQVR4nO3deXxU9b3/8dfMZJYsk30PIRAghE0QFUREFKn0Yl3r1Z9aBWurrdjbSluX0hZtVbzqz/qrYq1LxXtrRamituCuqCgosslmMIQlOwnZM8ms398fJwnEhGVCZs4sn+fjMY9zMpkwn0PCm5PvalBKKYQQQgScUe8ChBAiWkjgCiFEkEjgCiFEkEjgCiFEkEjgCiFEkEjgCiFEkEjgCiFEkMQE8818Ph9VVVXY7XYMBkMw31oIIQJCKUVrayu5ubkYjce+hw1q4FZVVZGfnx/MtxRCiKAoLy9nyJAhx3xNUAPXbrcDWmGJiYnBfGshhAiIlpYW8vPze/LtWIIauN3NCImJiRK4ImB8Ph979uwBYMSIEcf9NU+IwXAizaRBDVwhgqGjo4OioiIA2traiI+P17kiITQSuCIiJSUl6V2CEH1I4IqIEx8fT1NTk95lCNFHWDRuvfxlOZc/8SnPfFKmdylCiEi15r/hb9+F7a8E7C3CInAb211sOtDElvImvUsRQkSqii/gwDrobAnYW4RF4BZla8Mtdte26lyJ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z13NSpU9XNN98c0DpPhr/X+G0ej0fZ7Xb1/PPPB6rEQTGQ6/R4POqss85SzzzzjJo3b17IB66/1/iXv/xFFRYWKpfLFawSB4W/17lgwQI1a9asXs8tXLhQTZ8+PaB1DpYTCdzbb79djRs3rtdzV111lZozZ45f76Vbk0L3DsCzZ8/uee5YOwADrFu3rtfrAebMmXPU1+ttINf4bQ6HA7fbTWpqaqDKPGkDvc4//OEPZGZmcuONNwajzJMykGt84403mDZtGgsWLCArK4vx48dz//33h/T6vAO5zrPOOouNGzf2NDuUlZWxevVq5s6dG5Sag2Gwsieoq4Udyd8dgAFqamr6fX1NTU3A6jwZA7nGb7vjjjvIzc3t880OJQO5zrVr1/Lss8+yZcuWIFR48gZyjWVlZXzwwQdce+21rF69mtLSUm655RbcbjeLFy8ORtl+G8h1XnPNNdTX13P22WejlMLj8fCTn/yE3/zmN8EoOSiOlj0tLS10dHQQG3v0rdqPpHunmTi6Bx54gOXLl7Ny5UpsNpve5Qya1tZWrrvuOp5++mnS09P1LidgfD4fmZmZPPXUU5x22mlcddVVLFq0iCeffFLv0gbVmjVruP/++3niiSfYtGkTr776KqtWreKPf/yj3qWFHN3ucP3dARggOzvbr9frbSDX2O3hhx/mgQce4L333uOUU04JZJknzd/r3LNnD/v27eOiiy7qec7n8wEQExNDSUkJI0aMCGzRfhrI9zInJwez2YzJZOp5bsyYMdTU1OByubBYLAGteSAGcp2/+93vuO666/jRj34EwIQJE2hvb+emm25i0aJFx12UOxwcLXsSExNP+O4WdLzDHcgOwNOmTev1eoB33303ZHcMHuguxw8++CB//OMfeeuttzj99NODUepJ8fc6i4uL2bZtG1u2bOl5XHzxxZx33nls2bIlJLdhGsj3cvr06ZSWlvb8ZwKwe/ducnJyQjJsYWDX6XA4+oRq938yKkLWxhq07PGvP29wLV++XFmtVrVs2TK1c+dOddNNN6nk5GRVU1OjlFLquuuuU3feeWfP6z/99FMVExOjHn74YbVr1y61ePHisBgW5s81PvDAA8pisah//vOfqrq6uufR2tqq1yWcEH+v89vCYZSCv9d44MABZbfb1a233qpKSkrUv//9b5WZmanuvfdevS7hhPh7nYsXL1Z2u129+OKLqqysTL3zzjtqxIgR6sorr9TrEo6rtbVVbd68WW3evFkB6pFHHlGbN29W+/fvV0opdeedd6rrrruu5/Xdw8J+/etfq127dqmlS5eG37AwpZR67LHH1NChQ5XFYlFTpkxR69ev7/nczJkz1bx583q9/uWXX1ZFRUXKYrGocePGqVWrVgW5Yv/5c40FBQUK6PNYvHhx8Av3k7/fyyOFQ+Aq5f81fvbZZ2rq1KnKarWqwsJCdd999ymPxxPkqv3nz3W63W519913qxEjRiibzaby8/PVLbfcohobG4Nf+An68MMP+/131n1d8+bNUzNnzuzzNZMmTVIWi0UVFhaq5557zu/3lfVwhRAiSMK/NVsIIcKEBK4QQgSJBK4QQgSJBK4QQgSJBK4QQgSJBK4QQgSJBK4QQgSJBK4QQgSJBK4QQgSJBK4QQgSJBK4QQgTJ/wdaL4b0Z+jcmQAAAABJRU5ErkJggg=="
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = np.arange(0.01, 1, 0.02)\n",
    "y1 = -np.log(x)\n",
    "y0 = -np.log(1-x)\n",
    "plt.figure(figsize=(4, 2))\n",
    "plt.plot(x, y1)\n",
    "plt.plot(x, y0)\n",
    "plt.axvline(x=0.5, color='black', linestyle=':')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如图，当 $p$ 接近于0时, $-log(p)$ 会变得非常大,所以如果模型估算一个正类实例的概率接近于0,成本将会变得很高。同理估算出一个负类实例的概率接近1,成本也会变得非常高。\n",
    "\n",
    "那么反过来,当 $p$ 接近于1的时候, $-log(p)$ 接近于0,所以对一个负类实例估算出的概率接近于0,对一个正类实例估算出的概率接近于1,而成本则都接近于0,这不正好是我们想要的吗?\n",
    "\n",
    "值得注意的是，当y=0时, $$-log(1-p) = - [ ylog(p) + (1-y)log(1-p) ]$$\n",
    "y=1时同样如此，所以可以将分段函数合为一个公式。得到成本函数：\n",
    "$$J(\\theta) = - \\frac{1}{m} \\sum_{i=1}^{m}[ ylog(p) + (1-y)log(1-p) ]$$\n",
    "\n",
    "但是坏消息是,这个函数没有已知的闭式方程(不存在一个标准方程的等价方程)来计算出最小化成本函数的θ值。而好消息是这是个凸函数,所以通过梯度下降(或是其他任意优化算法)保证能够找出全局最小值(只要学习率不是太高,你又能长时间等待)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在给出其偏导公式，求出并没有看起来那么困难，把$p$展开为$\\sigma(wx+b)$,求起来就简单了：\n",
    "首先定义 $error= \\hat{y} - y$，$\\hat{y}$为y的预测值，为了理解下面的公式，别忘了$\\hat{y} = wx+b$\n",
    "$$\\frac{\\partial{J}}{\\partial{w}} = \\frac{1}{m} \\sum_{i=1}^{m}error \\cdot x$$\n",
    "$$\\frac{\\partial{J}}{\\partial{b}} = \\frac{1}{m} \\sum_{i=1}^{m}error$$\n",
    "\n",
    "现在我们已经了解了实现逻辑回归需要的知识。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 实操"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面我将使用一个非常有名的数据集——鸢尾植物数据集——来说明逻辑回归共有150朵鸢尾花,分别来自三个不同品种(山鸢尾、变色鸢尾和维吉尼亚鸢尾),数据里包含花的 _萼片以及花瓣的长度和宽度_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-20T21:36:05.783478Z",
     "end_time": "2023-04-20T21:36:05.798042Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)\n",
      "0                5.1               3.5                1.4               0.2\n",
      "1                4.9               3.0                1.4               0.2\n",
      "2                4.7               3.2                1.3               0.2\n",
      "3                4.6               3.1                1.5               0.2\n",
      "4                5.0               3.6                1.4               0.2\n",
      "[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n",
      " 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]\n"
     ]
    }
   ],
   "source": [
    "from sklearn import datasets\n",
    "import pandas as pd\n",
    "iris = datasets.load_iris()\n",
    "df = pd.DataFrame(data=iris.data, \n",
    "                  columns=iris.feature_names) #读取数据\n",
    "save = ~(iris.target == 2) #保存类别不为2的花\n",
    "X = df[save] #取出所有不是维吉尼亚鸢尾的花,即只留下两类花对应0,1\n",
    "y = iris.target[save]\n",
    "print(X.head())\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实现逻辑回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-20T21:36:06.806170Z",
     "end_time": "2023-04-20T21:36:09.162421Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测结果为：[1 0 1 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0]\n",
      "真实结果为：[1 0 1 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0]\n",
      "方差为：0.0\n"
     ]
    }
   ],
   "source": [
    "from sklearn import datasets\n",
    "import pdb\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "df = pd.DataFrame()\n",
    "\n",
    "def logisticRegression(x:np.ndarray, y:np.ndarray, num_iter=5000, alpha=0.01):\n",
    "    w, b = np.zeros(4), 0\n",
    "    m = w.shape[0]\n",
    "\n",
    "    for _ in range(num_iter):\n",
    "        z = x.dot(w) + b\n",
    "        y_hat = 1 / (1 + np.e**(-z))\n",
    "        error = np.array(y_hat - y)\n",
    "        der_w = 1/m * np.sum(x.T.dot(error))\n",
    "        der_b = 1/m * np.sum(error)\n",
    "        w = w - (alpha * der_w)\n",
    "        b = b - (alpha * der_b)\n",
    "\n",
    "    return w, b\n",
    "\n",
    "def predict(x:pd.DataFrame, w:np.ndarray, b:float):\n",
    "    z = x.dot(w) + b\n",
    "    tmp = 1 / (1 + np.e**(-z))\n",
    "    y_hat = list()\n",
    "    for i in tmp:\n",
    "        if i < 0.5:\n",
    "            y_hat.append(0)\n",
    "        else:\n",
    "            y_hat.append(1)\n",
    "    y_hat = np.array(y_hat)\n",
    "    return y_hat\n",
    "\n",
    "def error_variance(y_hat:np.ndarray, y_test:np.ndarray):\n",
    "    m = y_hat.shape[0]\n",
    "    variance = 1/m * np.sum((y_hat - y_test)**2)\n",
    "    return variance\n",
    "\n",
    "\n",
    "if '__main__' == __name__:\n",
    "    iris = datasets.load_iris()\n",
    "    df = pd.DataFrame(data=iris.data,\n",
    "                    columns=iris.feature_names) #读取数据\n",
    "    save = ~(iris.target == 2) #保存类别不为2的花\n",
    "    X = df[save] #去除维吉尼亚鸢尾花,只留下两类花对应0,1\n",
    "    y = iris.target[save]\n",
    "\n",
    "    x_train, x_test, y_train, y_test = train_test_split(\n",
    "        X, y, test_size=0.2) #已8:2拆分数据集为训练集和数据集\n",
    "\n",
    "    w, b = logisticRegression(x_train, y_train) #逻辑回归返回y=wx+b中的w,b\n",
    "    y_hat = predict(x_test, w, b) #使用测试集进行测试\n",
    "    variance = error_variance(y_hat, y_test)\n",
    "    print(f'预测结果为：{y_hat}')\n",
    "    print(f'真实结果为：{y_test}')\n",
    "    print(f\"方差为：{variance}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "由上面我们可以看到，预测可以说几乎完全正确，不止是程序，这应该也是数据集比较精良的结果"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
